package core.network.http.handler;

import business.global.config.BanCityMgr;
import business.rocketmq.constant.MqTopic;
import com.ddm.server.common.CommLogD;
import com.ddm.server.common.rocketmq.MqAbsBo;
import com.ddm.server.common.rocketmq.MqProducerMgr;
import com.ddm.server.common.utils.StringUtil;
import com.ddm.server.http.annotation.RequestMapping;
import com.ddm.server.http.server.HttpRequest;
import com.ddm.server.http.server.HttpResponse;
import com.ddm.server.http.server.HttpUtils;
import com.ddm.server.websocket.def.ErrorCode;
import com.google.gson.JsonObject;

import core.network.http.proto.ZleData_Result;


/**
 * 禁止城市
 *
 * @author Administrator
 */
public class BanCityRequest {

    /**
     * 重新禁止城市配置
     */
    @RequestMapping(uri = "/reloadBanCityConfig")
    public void reloadBanCityConfig(HttpRequest request, HttpResponse response) throws Exception {
        try {
            MqProducerMgr.get().send(MqTopic.HTTP_RELOAD_BAN_CITY_CONFIG, new MqAbsBo());
            response.response(ZleData_Result.make(ErrorCode.Success, "success"));
        } catch (Exception e) {
            CommLogD.error("reloadBanCityConfig error : {}", e.getMessage());
            response.response(ZleData_Result.make(ErrorCode.ErrorSysMsg, "null"));
        }

    }


    /**
     * 添加禁止城市
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(uri = "/putBanCity")
    public void putBanCity(HttpRequest request, HttpResponse response) throws Exception {
        String jsonPost = StringUtil.GetStringBody(request.getRequestBody());
        JsonObject dataJson = HttpUtils.abstractGMParams(jsonPost, HttpUtils.ZLE_KEY_RECHARGE);
        int value = HttpUtils.getInt(dataJson, "value");
        int type = HttpUtils.getInt(dataJson, "type");
        int banType = HttpUtils.getInt(dataJson, "banType");

        if (BanCityMgr.getInstance().put(value,type,banType)) {
            response.response(ZleData_Result.make(ErrorCode.Success, "success"));
        } else {
            response.response(ZleData_Result.make(ErrorCode.NotAllow, "error"));
        }
    }

    /**
     * 删除禁止城市
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(uri = "/deleteBanCity")
    public void deleteBanCity(HttpRequest request, HttpResponse response) throws Exception {
        String jsonPost = StringUtil.GetStringBody(request.getRequestBody());
        JsonObject dataJson = HttpUtils.abstractGMParams(jsonPost, HttpUtils.ZLE_KEY_RECHARGE);
        int value = HttpUtils.getInt(dataJson, "value");
        int type = HttpUtils.getInt(dataJson, "type");

        if (BanCityMgr.getInstance().delete(value,type)) {
            response.response(ZleData_Result.make(ErrorCode.Success, "success"));
        } else {
            response.response(ZleData_Result.make(ErrorCode.NotAllow, "error"));
        }
    }

}
